package com.example.demo.service.impl;

import com.example.demo.domain.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Service
@Component
public class UserServiceImpl implements UserService {

    private UserMapper userMapper;

    @Autowired
    public void  setUserMapper(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    /**
     * 登录功能
     * @param username
     * @param pwd
     * @return
     */
    @Override
    public User login(String username, String pwd) {
        return userMapper.login(username,pwd);
    }

    /**
     * 注册功能
     * @param user
     * @return
     */
    @Override
    public boolean register(User user) {
        //当传过来为空值的时候, 就不用直接进行插入了
        if((user.getPwd() == null || user.getPwd() == "") && (user.getUserName() == null || user.getUserName() =="")
            && user.getGender() == null){
            return false;
        }
        return userMapper.register(user);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    /**
     * 修改密码的功能
     * @param user
     * @return
     */
    @Override
    public boolean revise(User user) {
        return userMapper.revise(user);
    }

    /**
     * 判断用户是否存在
     * @param id
     * @param oldPwd
     * @return
     */
    @Override
    public User findByUserByPwd(int id, String oldPwd) {
        return userMapper.findByUserByPwd(id,oldPwd);
    }
}
